package org.example.chapter2;

import org.example.model.FileModel;
import rx.Observable;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

import java.io.File;
import java.util.Arrays;
import java.util.List;

public class Demo {

    public static void main(String[] args) {
        List<File> folders = Arrays.asList(new File("E:\\BingWallPaper"), new File("C:\\Users\\JohnConnor\\Pictures"));
        Observable.from(folders)
                .flatMap(new Func1<File, Observable<File>>() {
                    @Override
                    public Observable<File> call(File file) {
                        return Observable.from(file.listFiles());
                    }
                })
                .filter(new Func1<File, Boolean>() {
                    @Override
                    public Boolean call(File file) {
                        return file.getName().endsWith(".jpg");
                    }
                })
                .map(new Func1<File, FileModel>() {
                    @Override
                    public FileModel call(File file) {
                        FileModel fileModel = new FileModel();
                        fileModel.setFile(file);
                        fileModel.setFileSize(file.length());
                        return fileModel;
                    }
                })
                .subscribe(new Action1<FileModel>() {
                    @Override
                    public void call(FileModel fileModel) {
                        System.err.println("filePath:" + fileModel.getFile().getAbsolutePath());
                        System.err.println("fileSize:" + fileModel.getFileSize());
                    }
                })

        ;
    }
}
